<?php
session_start(); // Initialize Session data
ob_start(); // Turn on output buffering

include "ewcfg7.php";
include "ewmysql7.php";
include "phpfn7.php";
include "../userfn.php";

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0

$currentObject=array();
if (isLocal()){
error_reporting(E_ALL);
ini_set('display_errors', '1');
}
// Start timer
$GLOBALS["gsTimer"] = new cTimer();

// Open connection
$GLOBALS["conn"] = ew_Connect();
/*
 * IMPORTANT NOTE: This generated file contains only a subset of huge amount
 * of options that can be used with phpMyEdit. To get information about all
 * features offered by phpMyEdit, check official documentation. It is available
 * online and also for download on phpMyEdit project management page:
 *
 * http://platon.sk/projects/main_page.php?project_id=5
 *
 * This file was generated by:
 *
 *                    phpMyEdit version: 5.7.1
 *       phpMyEdit.class.php core class: 1.204
 *            phpMyEditSetup.php script: 1.50
 *              generating setup script: 1.50
 */

// MySQL host name, user name, password, database, and table

getPortalAppVars();
getAppLocales();
$_REQUEST["portalId"]=getPortalId();

//echo "portal ".getParameter("portalId")." ".;


// User profile
$UserProfile = new cUserProfile();
$UserProfile->LoadProfile(@$_SESSION[EW_SESSION_USER_PROFILE]);

// Security
$Security = new cAdvancedSecurity();
if (!$Security->IsLoggedIn()) $Security->AutoLogin();
if ($Security->IsLoggedIn()!=true && getParameter("action")!="login") {
	//$Security->SaveLastUrl();
	redirect("/butterflyportal/index.php?action=login");

}





include 'header.php';

if (getParameter("action")=="login")
{
	$portletUID="login_";
	//login
	?>
 	

 	<?php
//echo "ok ".IsLoggedIn();
if (!IsLoggedIn()) {

   /*// $clientId=getClientIdByMail(getSession("login"));

if ($clientId)
{

	header("Location: ".createNiceUrl("pageName","client_area"));
	exit;
}

if (getParameter("login") && getParameter("password") && !getParameter("SubmitLogin"))
{
	portalLogin(getParameter("login"),TEAdecrypt(getParameter("password"),KEY));

}
*/


?>
<script type="text/javascript">
$(document).fullScreen(true);
</script>

<div id="login">


<!-- <h1><?php echo $var_portal_login; ?></h1>  -->

    <?php


    if (getParameter("resendactivationlink")){

        $mail=getParameter("resendactivationlink");

       // echo $mail;
        $key=TEAencrypt($mail,KEY);
        $host=$_SERVER['HTTP_HOST'];
        if (isLocal())  $host=getPortalUrl().".betasite.net";
        $link="http://".$host."/?pageName=client_area&key=".$key;


        $data=array();
        $data["link"]=$link;
        $data["portal"]=getPortalName();
        $data["customer"]=getUserFullname();
        sendMailTo($mail, TemplateWithData($var_portal_mail_accountsaved,$data));
        ShowSuccess($var_portal_activation_sent);
    }

    $error=false;


    if (getParameter("SubmitLogin"))

    {

        if (getParameter($portletUID."email")==null ) displayError($var_portal_validmailerror,$portletUID."email");
        if (getParameter($portletUID."passwd")==null ) displayError($var_portal_invalidpassword,$portletUID."passwd");

        if ($error==false)
        {
            $user=getParameter($portletUID."email");
            $password=getParameter($portletUID."passwd");

            //$redirect=createNiceUrl("pageName",getParameter("pageName"));
            
            $redirect="?table=".getDefaultAdminPage()."&filter=1&succes=Loggedin successfully";

            if (getParameter("backurl")!=null) $redirect=decryptUrl(getParameter("backurl"));
           $validpwd=portalLogin($user,$password,$redirect);

           if ($validpwd){
         if ($redirect!=null) {
         	header("Location: ".$redirect);
         	exit;
         }}else{
         	
ShowError("Error");
         }
         
   
        /*	
        	$redirect=createNiceUrl("pageName",$portlet_portal_login_Back_Url,"type","succes","key","loggedin");
         	header("Location: ".$redirect);
        	exit;*/
      
         
       //if ($validpwd) ShowSuccess("Logged");
        }

    }

  //  echo $error;
    if ((getParameter("SubmitLogin")!=null && $error==true) || getParameter("SubmitLogin")==null) {


	if (getParameter("lostpassword")){
		
				$error=false;

				$mail=getParameter($portletUID."email");
				if ($mail==null || ew_CheckEmail($mail)==FALSE) displayError($var_portal_validmailerror,$portletUID."email");


                       // echo getClientIdByMail($mail);
                 if (getClientIdByMail($mail)=="") {

                     ShowError($var_portal_accountnotfound);
                     $error=true;
                 }

				if (!$error){




		$host=EW_PORTAL_URL;
		if (isLocal())  $host=getPortalUrl().".betasite.net";
		$link="http://".$host;

                    $data=array();
                    $data["link"]=$link;
                    $data["email"]=$mail;
                    $data["username"]=getUserFullname($mail);
                    $data["password"]=getUserPassword($mail);
                    sendMailTo($mail, TemplateWithData($var_portal_mail_passwordsent,$data));

				ShowSuccess($var_portal_password_sent);
				}
			}
			
			
			$url=addParameter("action", "login","");
			?>
        <form class="form-horizontal" id="login_form" method="post" action="<?=$url?>">
            <input type="hidden" name="backurl" value="<?=getParameter("backurl")?>">
            <!-- <h3><?php echo $var_portal_Alreadyin; ?></h3> -->
            <div class="control-group">
                <label class="control-label" for="inputEmail"><?php echo $var_portal_login; ?></label>
                <div class="controls">

                    <?php formText($portletUID."email",0,"account_input"); ?>
                </div>
            </div>
            <div class="control-group">
                <label class="control-label" for="inputPassword"><?php echo $var_portal_password; ?></label>
                <div class="controls">
                    <?php formPassword($portletUID."passwd",0,"account_input"); ?>
                </div>
            </div>
            <div class="control-group">
                <div class="controls">
                    <label class="checkbox">
                        <input type="checkbox"> Remember me
                    </label>
                    <button type="submit" class="btn" name="SubmitLogin" id="SubmitLogin" value="1"><?php echo $var_portal_Login; ?></button>
                    <button type="submit" class="btn"  name="lostpassword" id="lostpassword" value="1"><?php echo $var_portal_passwordforgot; ?></button>
                    <!-- <p class="lost_password"><a href="<?php echo  createUrl("action","lostpassword",$portletUID."email",getParameter($portletUID."email"));?>">Mot de passe oublié ?</a>  -->
                </div>
            </div>
        </form>









<?php
    }
}else{

    $redirect=createNiceUrl("pageName",$portlet_portal_login_Back_Url);
	header("Location: ".$redirect);


}
?>
 	
 	<?php  }elseif(getParameter("action")=="logout")
 	
 	{
 		
 		
 		if (IsLoggedIn())
 		{
 			
 			
 			portalLogout();
 			
 		?>
 		<script type="text/javascript">





document.cancelFullScreen(); 
 

document.webkitCancelFullScreen(); 
 

document.mozCancelFullScreen(); 

document.exitFullscreen();


</script>
 		
 		<?php 
 			
 			$redirect="?action=login&succes=Logged out!";

 				
 			if ($redirect!=null) {
 				header("Location: ".$redirect);
 				exit;
 			}
 		}
 	}elseif (getParameter("action")=="changepwd"){
 
	$portletUID="login_";
	
	if (getParameter($portletUID."passwd")!="")
	{
	
		if (getParameter($portletUID."passwd")==getParameter($portletUID."confirmpasswd")) {
			


		
	updateObjByKey("portal_logins","id",getCurrentUserId(),"password",encryptString(getParameter($portletUID."passwd")));


ShowSuccess("Password changed");
		}else{
			ShowError("Password not changed");

		}
	}else{
	$url=addParameter("action", "changepwd","");
	?>
	
		<form class="std  " id="login_form" method="post" action="<?=$url?>">

	<!-- <h3><?php echo $var_portal_Alreadyin; ?></h3> -->
		<fieldset>

		
			<p class="text">
				<label for="passwd"><?php echo $var_portal_password; ?></label>
				<span><?php formPassword($portletUID."passwd",0,"account_input"); ?></span>
			</p>
			<p class="text">
				<label for="passwd">Confirmation<?php //Cecho $var_portal_password; ?></label>
				<span><?php formPassword($portletUID."confirmpasswd",0,"account_input"); ?></span>
			</p>
			
			<p class="submit">
								
				<input type="submit" value="<?php echo $var_portal_submit; ?>" class="button" name="SubmitLogin" id="SubmitLogin">
				
			</p>
		
		</fieldset>

	</form>
	
	<?php 
		}
 	}else{


 
if (getParameter("table")) setParameterInSession("table");
$tablename=getParameterFromSession("table");

//echo $tablename;
//R::debug(true);
$tableobject=R::findOne("portal_objects","name = ? ",array($tablename));


if ($tableobject->redirecturl!="") redirect($tableobject->redirecturl);

// Search
// backurl:/butterflyportal/index.php
// backurl:/butterflyportal/index.php?table=cabinet_dossier&filter=1
// table:cabinet_dossier
// portalId:24
// portalId:24
// ButterflyPortal_sys_sfn[0]:0
// ButterflyPortal_sys_fl:1
// ButterflyPortal_sys_qfn:
// ButterflyPortal_sys_fm:0
// ButterflyPortal_sys_qf1:
// ButterflyPortal_sys_qf2:ag
// ButterflyPortal_sys_qf3:
// ButterflyPortal_sys_qf6:
// ButterflyPortal_sys_qf11:
// ButterflyPortal_sys_qf12:
// ButterflyPortal_sys_qf13:
// ButterflyPortal_sys_qf14:
// ButterflyPortal_sys_qf16:
// ButterflyPortal_sys_filter:Filter
// ButterflyPortal_sys_rec:24
// ButterflyPortal_sys_navfmdown:0

if(TableExist($tablename) || isMappedTable($tablename)){

require_once 'phpMyEdit.class.php';

//
if (getParameter("showmaster") )
{

	
	$mastertable=getParameter("showmaster");

	//echo $mastertable;
	$tmptable=getParameter("table");
	

	setParameter("table", $mastertable);
	include 'phpMyEdit.init.php';

	new phpMyEdit($opts);
	//echo "yyyy".$tmptable;
	setParameter("table", $tmptable);
	//echo "xxxxxxxxxxxxxxx".getParameter("table");
}

include 'phpMyEdit.init.php';

// Now important call to phpMyEdit

new phpMyEdit($opts);
}

else{
	


$folder=preg_split("/_/", $tablename)[0];
$file="system/".$folder."/".$tablename.".php";
	
if (file_exists($file)) include $file;


}
}


include 'footer.php';
?>